package com.example.demo.crypto;

import com.example.demo.util.AesEncryptUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.util.StreamUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class DecryptHttpInputMessage implements HttpInputMessage {
    private final HttpInputMessage inputMessage;

    public DecryptHttpInputMessage(HttpInputMessage inputMessage) {
        this.inputMessage = inputMessage;
    }

    @Override
    public InputStream getBody() throws IOException {
        String content = StreamUtils.copyToString(inputMessage.getBody(), StandardCharsets.UTF_8);

        String decryptBody = AesEncryptUtils.decrypt(content);

        return new ByteArrayInputStream(decryptBody.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public HttpHeaders getHeaders() {
        return inputMessage.getHeaders();
    }
}